<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
		<style type="text/css">
		<!--
			*{ padding:0px; margin:0px;}
		-->
		</style>
		<title>Sample Chat</title>
		<script src="https://static.robotwebtools.org/EventEmitter2/current/eventemitter2.js"></script>
		<script src="https://static.robotwebtools.org/roslibjs/current/roslib.js"></script>	</head>
	<body>
		<script type="text/javascript">
		<!--
if(!Chat){
	var Chat = {
		ros : null,
		name : "",
		init : function(){
			this.ros = new ROSLIB.Ros();
			this.ros.on('error', function(error) {
				document.getElementById('state').innerHTML = "Error";
				document.getElementById("btn").disabled = true;
			});
			this.ros.on('connection', function(error) {
				document.getElementById('state').innerHTML = "Connect";
				document.getElementById("btn").disabled = false;
			});
			this.ros.on('close', function(error) {
				document.getElementById('state').innerHTML = "Close";
				document.getElementById("btn").disabled = true;
			});
			this.ros.connect('ws://localhost:9090');

			var sub = new ROSLIB.Topic({
				ros : this.ros,
				name : '/chatter',
				messageType : 'std_msgs/String'
			});
			sub.subscribe(function(message) {
				var res = message.data;
				var contents = ""
				if(res.match(/(\[.+\])(.*):(.*)/)){
					var str_date = RegExp.$1;
					var name = RegExp.$2;
					var comment = RegExp.$3;
					contents = str_date + " <b>" + name + " :</b> " + comment;
				}else{
					contents = res;
				}
				var el = document.createElement("p");
				el.innerHTML = contents;
				//document.getElementById("talk").appendChild(el);
				document.getElementById("talk").insertBefore(el, document.getElementById("talk").childNodes[0]);

				document.getElementById("btn").disabled = false;
				document.getElementById("state").innerHTML = "received";
			});
		},
		send : function(){
			if(document.getElementById("comment").value.length == 0) return;

			document.getElementById("btn").disabled = true;
			document.getElementById("state").innerHTML = "sending";

			var now = new Date();
			var str_date = (now.getMonth()<10?"0":"") + (now.getMonth()+1) + "/";
			str_date += (now.getDate()<10?"0":"") + now.getDate() + "/";
			str_date += now.getFullYear() + " ";
			str_date += (now.getHours()<10?"0":"") + now.getHours() + ":";
			str_date += (now.getMinutes()<10?"0":"") + now.getMinutes() + ":";
			str_date += (now.getSeconds()<10?"0":"") + now.getSeconds(); 

			var comment = document.getElementById("comment").value;
			var pub = new ROSLIB.Topic({
				ros : this.ros,
				name : '/chatter',
				messageType : 'std_msgs/String'
			});

			var str = new ROSLIB.Message({data : "[" + str_date + "]" + this.name + ":" + comment});
			pub.publish(str);
			document.getElementById("comment").value = "";
		}
	}
	Chat.init();

	window.onload = function(){
		Chat.name = window.prompt("What's your name?", "Jane Doe");
		document.getElementById("name").innerHTML = Chat.name;
	};
	window.onunload = function(){
		Chat.ros.close();
	};
}
		//-->
		</script>
<p>Connection status： <label id="state">Disconnect</label></p>
<p>Your name： <label id="name"></label></p>
<form action="javascript:Chat.send();" id="ui">
<label>Comment: </label>
<input type="text" id="comment" size="20" />
<input type="submit" value="send" id="btn" /><br/>
</form>
<div id="talk"><p></p></div>

	</body>
</html>

